function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}


$(function(){
    var collectstatus = $('#collect_status').val();
    var focusstatus = $('#focus_status').val();
    var collection = $('.collection');
    var collected = $('.collected');
    var focus = $('.focus');
    var focused = $('.focused');
    $(document).ready(function () {
        if(collectstatus==0){
            collection.show();
            collected.hide()
        }
        else{
            collected.show();
            collection.hide()
        }
        if(focusstatus==0){
            focus.show();
            focused.hide()
        }
        else{
            focused.show();
            focus.hide()
        }
    })

    // 打开登录框
    $('.comment_form_logout').click(function () {
        $('.login_form_con').show();
    })

    // 收藏
    collection.click(function () {

        $.post(
            '/news',
            {
                'csrf_token': document.getElementById('csrf_token').value,
               'newsid':document.getElementById('newsid').value
            },
            function (code) {
                alert(code);
            }

        );
        collected.show();
        collection.hide();


    });  

    // 取消收藏
    collected.click(function () {
        $.post(
            '/news',
            {
                'csrf_token': document.getElementById('csrf_token').value,
               'newsid': document.getElementById('newsid').value
            },
            function (code) {
                alert(code);
            }

        );
        collection.show();
        collected.hide();

    });

        // 评论提交
    $(".comment_form").submit(function (e) {
        e.preventDefault();

    });

    $('.comment_list_con').delegate('a,input','click',function(){

        var sHandler = $(this).prop('class');

        if(sHandler.indexOf('comment_reply')>=0)
        {
            $(this).next().toggle();
        }

        if(sHandler.indexOf('reply_cancel')>=0)
        {
            $(this).parent().toggle();
        }

        if(sHandler.indexOf('comment_up')>=0)
        {
            var $this = $(this);
            if(sHandler.indexOf('has_comment_up')>=0)
            {
                // 如果当前该评论已经是点赞状态，再次点击会进行到此代码块内，代表要取消点赞
                $this.removeClass('has_comment_up')
            }else {
                $this.addClass('has_comment_up')
            }
        }

        if(sHandler.indexOf('reply_sub')>=0)
        {
            alert('回复评论')
        }
    })

        // 关注当前新闻作者
    focus.click(function () {

        $.post(
            '/news',
            {
                'csrf_token': document.getElementById('csrf_token').value,
               'news_creater_id': document.getElementById('news_creater').value
            },
            function (code) {
                alert(code);
            }
        );
        focused.show();
        focus.hide();

    });

    // 取消关注当前新闻作者
    focused.click(function () {

        $.post(
            '/news',
            {
                'csrf_token': document.getElementById('csrf_token').value,
               'news_creater_id': document.getElementById('news_creater').value
            },
            function (code) {
                alert(code);
            }
        );
        focus.show();
        focused.hide();

    })
});